import {PageGame} from './PageGame'
import {PageMain} from './PageMain'

export default class PageMonitor {
  constructor(_this) {
    this.game = new PageGame(_this);
    this.main = new PageMain(_this);
    this.connect = _this.connect;
    this.touching = false;
    this.touch = {x1: -1, y1: -1, x2: -1, y2: -1, x3: -1, y3: -1}
  }

  display(mode, engin) {
    switch (mode) {
      case 'load':
        // this.load.display(engin);
        break;
      case 'main':
        this.main.display(engin);
        break;
      case 'game':
        if (this.touching) {
          this.game.characterJudge(this.touch);
        }
        this.game.display(engin);
        engin.ctx.fillStyle = 'white';
        break;
      default:
        break;
    }
    engin.ctx.font = "20px Georgia";
    engin.ctx.fillText(this.connect.getWords(), 10, 30);
  }

  touchStart(event, mode) {
    this.touching = true;
    this.touch = {
      x1: event.touches[0].clientX,
      y1: event.touches[0].clientY,
      x2: event.touches.length > 1 ? event.touches[1].clientX : -1,
      y2: event.touches.length > 1 ? event.touches[1].clientY : -1,
      x3: event.touches.length > 2 ? event.touches[2].clientX : -1,
      y3: event.touches.length > 2 ? event.touches[2].clientY : -1,

    };
    this.game.characterJudge(this.touch);
    // console.log(this.touch);
    // console.log(event);
  }

  touchMove(event, mode) {
    this.touch = {
      x1: event.touches[0].clientX,
      y1: event.touches[0].clientY,
      x2: event.touches.length > 1 ? event.touches[1].clientX : -1,
      y2: event.touches.length > 1 ? event.touches[1].clientY : -1,
      x3: event.touches.length > 2 ? event.touches[2].clientX : -1,
      y3: event.touches.length > 2 ? event.touches[2].clientY : -1,
    };
    // this.touching
    //  console.log(event);
  }

  touchEnd(event, mode) {
    if (event.touches.length === 0) {
      this.touching = false;
      if (
        this.game.hero.status !== 'climbing' &&
        this.game.hero.status !== 'drop' &&
        this.game.hero.status !== 'sitdown' &&
        this.game.hero.status !== 'getup'
      ) {
        this.game.hero.status = 'stop';
      }
      this.game.hero.step = 0;
    } else {
      this.touch = {
        x1: event.touches[0].clientX,
        y1: event.touches[0].clientY,
        x2: event.touches.length > 1 ? event.touches[1].clientX : -1,
        y2: event.touches.length > 1 ? event.touches[1].clientY : -1,
        x3: event.touches.length > 2 ? event.touches[2].clientX : -1,
        y3: event.touches.length > 2 ? event.touches[2].clientY : -1,
      };
      // th
    }
    // console.log(event);
    switch (mode) {
      case 'load':
        break;
      case 'main':
        if (this.connect.getConnection()) {
          this.main.gamestart();
        }
        break;
      case 'game':
        break;
      default:
        break;
    }
  }
}
